package cate.game.play.skill.passive.awaken;

import cate.common.table.d.GDSkill;
import cate.common.util.XT;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.Buff;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.List;

/**
 * 效果1：每次进入觉醒状态时,随机选取3名敌方单位,将其1个增益状态转化为【无效增益】,该状态无实际效果但视为增益状态,持续2回合;
 *        觉醒状态下每次抗控成功则提升自身6%伤害减免(不可驱散),每回合至多触发2次,最多5层,持续到战斗结束
 * 效果2：【觉醒·肆意】额外提升10%伤害减免和10%暴伤减免;觉醒状态下回合开始前有70%概率随机增加1名友方单位25%觉醒值,40%概率增加自身25%觉醒值
 * 效果3：抗控成功增加的伤害减免提升至10%;觉醒状态下免疫非控制类负面状态
 */
public class 破魂游荡者PH extends PassiveHandler {

	private int 转化人数;
	private int 无效增益buff;

	private int 抗控buff;
	private int 抗控数量;

	private int 友方概率;
	private int 自身概率;
	private double 觉醒提升;



	//转化人数=3&无效增益buff=？？&抗控buff=？？&抗控数量=2&
	//友方概率=7000&自身概率=4000&觉醒提升=25&
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		转化人数 = args.getInt("转化人数", 0);
		无效增益buff = args.getInt("无效增益buff", 0);

		抗控buff = args.getInt("抗控buff", 0);
		抗控数量 = args.getInt("抗控数量", 0);

		友方概率 = args.getInt("友方概率", 0);
		自身概率 = args.getInt("自身概率", 0);
		觉醒提升 = args.getDouble("觉醒提升", 0d);
	}

	@Override
	public void onAwaken(ActionCtx action, boolean start) {
		if (start) {
			for (Fighter target : TargetSelector.doFind(skill.owner, action.getTeamVs(), new TargetFilterParam().targetNumSet(转化人数))) {
				List<Buff> buffs = target.buff.tryDispelGood(action, 1);
				if (XT.isNotBlank(buffs)) {
					target.buff.tryAddByTid(action, skill.owner, 无效增益buff);
				}
			}
		}
	}


	@Override
	public void onRoundBegin(ActionCtx action) {
		addNum = 0;
	}

	private int addNum;

	@Override
	public void onAntiCtrl(ActionCtx action, Buff buff) {
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		if (addNum >= 抗控数量) {
			return;
		}
		addNum++;
		skill.owner.buff.tryAddByTid(action, 抗控buff);
	}

	@Override
	public void beforeRoundBegin(ActionCtx action) {
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		if (XT.chance(友方概率)) {
			Fighter one = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam().scopeSet(GDSkill.Scope.FRIENDS));
			if (one != null) {
				one.skill.addAwakenEnergy(action, 觉醒提升);
			}
		}
		if (XT.chance(自身概率)) {
			skill.owner.skill.addAwakenEnergy(action, 觉醒提升);
		}
	}


}
